package org.vectrics.recruiter.struts.admin.job;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.ecs.html.Form;
import org.vectrics.common.html.Combo;
import org.vectrics.common.view.HtmlForm;
import org.vectrics.common.view.HtmlFormEditRow;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.struts.admin.InterviewForm;
import org.vectrics.recruiter.user.Interview;
import org.vectrics.recruiter.user.InterviewInstance;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.PersonEncounterStatusType;

public class InterviewEditFormTag extends VectricsTagSupport {

    private static final long serialVersionUID = -4637609432700516456L;

    public static String FORM_ID = "interview_edit_form";

    @Override
    protected String makeStartTag() {
        Interview interview = null;
        // boolean autoRefId =
        // RecruitConfig.getRecruitInstance().getAutoJobReferenceId(ThreadProperties.getDomain());
        InterviewForm actionForm = (InterviewForm) getRequest().getAttribute("interviewForm");

        HtmlForm form = new HtmlForm(getRequest(), "interviewForm", this.getPhrase("phrase.interview.information"));
        form.setId(FORM_ID);
        form.setMethod(Form.POST);
        form.setAction("/hrs-intranet/admin/jobs/candidate/interview/save.do");
        form.addHidden("id", actionForm.getId());
        form.addHidden("jobId", actionForm.getJobId());
        form.addHidden("personId", actionForm.getPersonId());
        if (actionForm.getId() != null) {
            form.addHidden("id", actionForm.getId());
            interview = RecruitServiceLocator.getRecruitUserService().findInterviewById(new Long(actionForm.getId()));
        }

        if (interview == null) {
            actionForm.setInterviewerId(ThreadProperties.getPerson().getId().toString());
            actionForm.setStatusCode("sched");
        }

        form.addReadOnlyText("jobTitle", this.getPhrase("prompt.job.title"), actionForm.getJobTitle());
        form.addPersonPicker(this.getPhrase("prompt.interview.interviewer"), "interviewerId", getContextPath(), true);

        form.addDatePickerForParam(this.getPhrase("prompt.date") + " *", "startDate");

        form.addTimeEditForParam(this.getPhrase("prompt.time"), "startHour", "startMinute", "startAmPm", true);

        HtmlFormEditRow minutesRow = (HtmlFormEditRow) form.addEditForParam(this.getPhrase("prompt.interview.minutes"),
                "minutes", false);
        minutesRow.getEditBox().setMaxlength(3);
        minutesRow.getEditBox().setOnKeyPress(" return soloNumerosEnteros(event, this);");
        
        
        List<InterviewInstance> interviewInsntaces = RecruitServiceLocator.getInterviewSearcher().getInterviewInstances();
        Combo interviewInsntacesCombo = new Combo();
        Iterator<InterviewInstance> interviewInsntacesIterator = interviewInsntaces.iterator();
        
        while (interviewInsntacesIterator.hasNext()) {
			InterviewInstance interviewInstance = (InterviewInstance) interviewInsntacesIterator.next();
			interviewInsntacesCombo.addOption(interviewInstance.getCode(), interviewInstance.getDescription());
		}
        try {
        	if (interview.getInterviewInstance() != null) {
            	interviewInsntacesCombo.selectOption(interview.getInterviewInstance().getCode());
            }
		} catch (Exception e) {
		}
        

        form.addComboRowForParam(this.getPhrase("prompt.interview.instance"), "interviewInstance", interviewInsntacesCombo, true);
        
        Collection statusTypes = RecruitServiceLocator.getPersonService().findAllPersonEncounterStatusTypes();
        Combo statusCombo = new Combo();
        Iterator iter = statusTypes.iterator();
        while (iter.hasNext()) {
            PersonEncounterStatusType status = (PersonEncounterStatusType) iter.next();
            statusCombo.addOption(status.getName(), status.getCode());
        }
        if (interview != null) {
            statusCombo.selectOption(interview.getStatusType().getCode());
        } else {
            // estado por default de la entrevista (COMPLETED)
            statusCombo.selectOption(PersonEncounterStatusType.STATUS_DONE);
            actionForm.setStatusCode(PersonEncounterStatusType.STATUS_DONE);
        }

        form.addComboRowForParam(this.getPhrase("prompt.interview.status"), "statusCode", statusCombo, true);

        form.addTextAreaRowForParam(this.getPhrase("prompt.interview.description"), "description", 6, 60, false);
        form.addTextAreaRowForParam(this.getPhrase("prompt.interview.notes"), "notes", 6, 60, false);
        form.addSubmitAndCancelButton();
        return form.toString();
    }

    /*
     * <html:form action="/admin/jobs/candidate/interview/save" method="POST"
     * focus="name"> <html:hidden property="id"/> <html:hidden
     * property="jobId"/> <html:hidden property="userId"/> <input type="hidden"
     * name="companyId" value="<%=companyId%>">
     * 
     * <table> <tr> <td class="formBg1" valign="top" width="150"> <font
     * class="formLabelClass">Date</font> <vectrics:errors property="dateStr"/>
     * </td> <td class="formBg1" valign="top"> <html:text property="dateStr"
     * styleClass="formEditClass" maxlength="40"/> </td> <tr> <td
     * class="formBg1" valign="top" width="150"> <font
     * class="formLabelClass">Status</font> </td> <td class="formBg1"
     * valign="top"> <html:select name="interviewForm" property="statusCode"
     * styleClass="formComboClass"> <html:options collection="statusTypes"
     * labelProperty="name" property="code"/> </html:select>
     * 
     * </td> </tr>
     * 
     * <tr> <td class="formBg1" valign="top"><font
     * class="formLabelClass">Description</font></td> <td class="formBg1"
     * valign="top"> <html:textarea rows="10" property="description"
     * styleClass="formTextAreaClass" cols="80"/> </td> </tr>
     * 
     * <tr> <td class="formBg1" valign="top"><font
     * class="formLabelClass">Notes</font></td> <td class="formBg1"
     * valign="top"> <html:textarea rows="10" property="notes"
     * styleClass="formTextAreaClass" cols="80"/> </td> </tr>
     * 
     * <tr> <td valign="TOP" align="left"></td> <td valign="TOP" align="left">
     * <vectrics:formButton code="button.cancel" phraseCode="button.cancel"
     * name="cancel"/>&nbsp; <vectrics:formButton code="button.ok"
     * phraseCode="button.ok" name="submit"/> </td> </tr> </table> </html:form>
     */
}
